﻿#light

module Dwarf_GraphTraversal_Generator

open System
open System.IO
open System.Text
open System.ComponentModel

let Dwarf_GraphTraversal_Generator (fs : FileStream, args : String[]) = 

    let sw = new StreamWriter(fs)
    let rand = new Random()
    
    let size = (int) args.[args.Length - 1]
    
    let sqrtOfSize = int(Math.Sqrt(float size))
    let maxEdgeCount  = min 100 sqrtOfSize
    // Write the number of vertices.
    sw.WriteLine(size);
    
    // create 2-dim array
    let of_array2 (mat:int[,]) =
        Array.init (Array2D.length1 mat) (fun i ->
        Array.init (Array2D.length2 mat) (fun j ->
           mat.[i,j]))

    // working initialization
    let arr = Array2D.create size maxEdgeCount -1 |> of_array2;

    // Count of current # edges for each vertex
    let edgeCount = Array.create size 0
    
    for i = 0 to size - 2 do
        if (edgeCount.[i] < maxEdgeCount) then
            for j = 0 to maxEdgeCount - 1 do
                let temp = rand.Next(i + 1 ,size); 
                if  ( (edgeCount.[i] < maxEdgeCount) && (edgeCount.[temp] < maxEdgeCount) && (Array.IndexOf(arr.[i], temp) < 0) ) then 
                    arr.[i].[edgeCount.[i]] <- temp;
                    edgeCount.[i] <- edgeCount.[i] + 1;
                    arr.[temp].[edgeCount.[temp]] <- i;
                    edgeCount.[temp] <- edgeCount.[temp] + 1;
    
    // Write The list of edges for each vertex line by line. 
    // The first number of line is index of vertex,index and list of edges separated by ":" simbol,
    // the edges in list separated by " " simbol.
    for i = 0 to size - 1 do
        sw.Write(i);
        sw.Write(":");
        for j = 0 to maxEdgeCount - 1 do
             if (arr.[i].[j] <> -1  ) then 
                if  j <> 0 then sw.Write(" ");
                sw.Write(arr.[i].[j]); 
                
        sw.WriteLine();
    sw.Close();
    true
    
